package org.linlinjava.litemall.wx.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.dao.LitemallDistributionBenefitMapper;
import org.linlinjava.litemall.db.domain.LitemallDistributionBenefit;
import org.linlinjava.litemall.db.domain.LitemallDistributionBenefitExample;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class WxDistributionBenefitService {
    @Autowired
    private LitemallDistributionBenefitMapper distributionBenefitMapper;
    @Autowired
    private LitemallUserService userService;


    public void create(LitemallDistributionBenefit distributionBenefit){
        distributionBenefit.setAddTime(LocalDateTime.now());
        distributionBenefit.setUpdateTime(LocalDateTime.now());
        distributionBenefitMapper.insertSelective(distributionBenefit);
    }

    /**
     * 获取分销受益记录
     * @param userId
     * @return
     */
    public List<LitemallDistributionBenefit> list(Integer userId,Integer page,Integer size,String sort,String order) {
        LitemallDistributionBenefitExample example = new LitemallDistributionBenefitExample();
        example.or().andBeneficiaryUserIdEqualTo(userId);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            example.setOrderByClause(sort+ " "+order);
        }
        PageHelper.startPage(page,size);
        return distributionBenefitMapper.selectByExample(example);
    }

    /**
     * 获取用户分销总计收益
     * @param userId
     * @return
     */
    public Map<String,Object> queryMyDistributionBenefit(Integer userId) {
        LitemallUser user = userService.findById(userId);

        Integer numberOfInvitations = user.getNumberOfInvitations();
        BigDecimal invitationBenefitAmount = user.getInvitationBenefitAmount();
        Integer invitationBenefitDays = user.getInvitationBenefitDays();

        Map<String, Object> data = new HashMap<>(5);
        data.put("numberOfInvitations",numberOfInvitations);
        data.put("invitationBenefitAmount",invitationBenefitAmount);
        data.put("invitationBenefitDays",invitationBenefitDays);
        return data;
    }




}
